﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components;

using ITAP.ContourBuilder.Components.Constraints;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Components.Constraints
{
    [Serializable]
    public class ConstraintJoinContourElementEnds : Constraint
    {
        private int connectedContourElementsCount = 0;
        public ConstraintJoinContourElementEnds(int connectedContourElementsCount)
        {
            this.connectedContourElementsCount = connectedContourElementsCount;
        }

        public override bool Check(IContourComponent component)
        {
            if (!enabled)
                return false;

            ModelPoint componentPosition = component.Position;
            int counter = 0;
            foreach (IContourComponent connectedComponent in component.ConnectedComponents)
            {
                if (connectedComponent is IEndedContourComponent)
                {
                    IEndedContourComponent endedConnectedComponent = connectedComponent as IEndedContourComponent;
                    if (endedConnectedComponent.StartConnectedComponent == component)
                    {
                        if (!endedConnectedComponent.StartPoint.Equals(componentPosition))
                            return false;
                    }
                    else if (endedConnectedComponent.EndConnectedComponent == component)
                    {
                        if (!endedConnectedComponent.EndPoint.Equals(componentPosition))
                            return false;
                    }

                }
                if (connectedContourElementsCount != 0)
                {
                    counter++;
                    if (counter == connectedContourElementsCount)
                        break;
                }
            }

            return true;
        }

        public override void Fix(IContourComponent component)
        {
            if (!enabled)
                return;

            ModelPoint componentPosition = component.Position;
            int counter = 0;
            foreach (IContourComponent connectedComponent in component.ConnectedComponents)
            {
                if (connectedComponent is IEndedContourComponent)
                {
                    IEndedContourComponent endedConnectedComponent = connectedComponent as IEndedContourComponent;
                    if (endedConnectedComponent.StartConnectedComponent == component)
                        endedConnectedComponent.MoveStartPoint(componentPosition);
                    else if (endedConnectedComponent.EndConnectedComponent == component)
                        endedConnectedComponent.MoveEndPoint(componentPosition);
                }
                if (connectedContourElementsCount != 0)
                {
                    counter++;
                    if (counter == connectedContourElementsCount)
                        break;
                }
            }
        }
    }
}
